package com.CST.service.impl.ys;

import com.CST.common.BaseEntity;
import com.CST.dto.ys.YsBusinessSubjectDTO;
import com.CST.entity.ys.YsBusinessSubject;
import com.CST.mapper.cst.ys.YsBusinessSubjectMapper;
import com.CST.service.ys.YsBusinessSubjectService;
import com.CST.service.ys.YsSubjectService;
import com.CST.util.ExcelTransfer;
import com.CST.vo.ys.YsBusinessSubjectListVO;
import com.CST.vo.ys.YsBusinessSubjectVO;
import com.CST.vo.ys.YsSubjectListVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author qtx
 * @since 2023-04-24
 */
@Service
public class YsBusinessSubjectServiceImpl extends ServiceImpl<YsBusinessSubjectMapper, YsBusinessSubject> implements YsBusinessSubjectService {

    private final YsSubjectService ysSubjectService;

    private final ExcelTransfer<YsBusinessSubjectVO> excelTransfer;

    public YsBusinessSubjectServiceImpl(@Lazy YsSubjectService ysSubjectService, ExcelTransfer<YsBusinessSubjectVO> excelTransfer) {
        this.ysSubjectService = ysSubjectService;
        this.excelTransfer = excelTransfer;
    }

    @Override
    public IPage<YsBusinessSubjectVO> listYsBusinessSubjectPage(YsBusinessSubjectDTO dto) {
        return baseMapper.selectPageNew(dto.getPage(), dto);
    }

    @Override
    public void exportExcelYsBusinessSubject(HttpServletResponse response, YsBusinessSubjectDTO dto) {
        excelTransfer.exportExcel(response, baseMapper.selectNew(dto), "业务职能科目对照", "sheet",
                YsBusinessSubjectVO.class);
    }

    @Override
    public void exportExcelYsBusinessSubjectDemo(HttpServletResponse response) {
        excelTransfer.exportExcel(response, new ArrayList<>(), "业务职能科目对照", "sheet",
                YsBusinessSubjectVO.class);
    }

    @Override
    public void importExcelYsBusinessSubject(MultipartFile file) {
        List<YsSubjectListVO> vos = ysSubjectService.listYsSubject();
        Map<String, Integer> map = vos.stream()
                .collect(Collectors.toMap(e -> e.getSubjectCode() + e.getSubjectName(), YsSubjectListVO::getId));
        excelTransfer.importExcel(file, YsBusinessSubjectVO.class, list -> {
            list.forEach(e -> {
                YsBusinessSubject ysBusinessSubject = new YsBusinessSubject();
                BeanUtils.copyProperties(e, ysBusinessSubject);
                Integer integer = map.get(e.getSubjectCode() + e.getSubjectName());
                ysBusinessSubject.setSubjectId(integer.longValue());
                ysBusinessSubject.setFlag(Boolean.TRUE);
                save(ysBusinessSubject);
            });
            return new ArrayList<>();
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean inherit(Integer source, Integer target) {
        List<YsBusinessSubject> list = list(Wrappers.lambdaQuery(YsBusinessSubject.class)
                .eq(BaseEntity::getDeleteFlag, Boolean.FALSE)
                .eq(YsBusinessSubject::getYear, source));
        if (list.isEmpty()) {
            throw new RuntimeException("源头年份数据为空。");
        }
        list.forEach(e -> {
            e.setYear(target);
            e.setId(null);
            save(e);
        });
        return true;
    }

    @Override
    public List<YsBusinessSubjectListVO> listYsBusinessSubject(Long subjectId, String name) {
        return baseMapper.selectNew1(subjectId, name);
    }

    @Override
    public Integer selectByIds(YsBusinessSubject entity) {
        return baseMapper.selectByIds(entity);
    }
}
